package com.example.mall.user.svc.domain.factory;

import com.example.mall.common.exception.BizException;
import com.example.mall.user.svc.app.cmd.CreateUserCmd;
import com.example.mall.user.svc.domain.model.entity.User;
import com.example.mall.user.svc.infr.mapper.UserMapper;

import java.util.Optional;

/**
 * @author Mickey
 * @since 2022/4/19
 **/
public class UserFactory {

    public static User instance(CreateUserCmd createUser, UserMapper userMapper) {
        Optional<User> optionalUser = userMapper.byUniqueId(createUser.getUsername());
        if (optionalUser.isPresent()) {
            throw new BizException("已有用户");
        }
        return createUser.converterToUser();
    }
}
